
package com.ruoyi.web.controller.coin;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.coin.service.ICoinBaseSqlService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.MjkjUtils;
import com.ruoyi.common.utils.blade.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 交易记录
 */
@Controller
@RequestMapping("/coin/tran")
public class CoinTranController extends BaseController {
    private String prefix = "coin/tran";

    @Autowired
    private ICoinBaseSqlService baseSqlService;


    @GetMapping("/index")
    public String index() {
        return prefix + "/index";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        Map<String, Object> parameterMap = MjkjUtils.getParameterMap(request);

        String hash = MjkjUtils.getMap2Str(parameterMap, "hash");
        String tran_state = MjkjUtils.getMap2Str(parameterMap, "tran_state");
        String is_sell = MjkjUtils.getMap2Str(parameterMap, "is_sell");

        Page page = getPage();
        QueryWrapper wrapper = MjkjUtils.getQueryWrapper();
        wrapper.eq("tran_type", "BUY");

        wrapper.eq(Func.isNotEmpty(hash), "hash", hash);
        wrapper.eq(Func.isNotEmpty(tran_state), "tran_state", tran_state);
        wrapper.eq(Func.isNotEmpty(is_sell), "is_sell", is_sell);
        wrapper.select("*", "DATE_FORMAT(tran_time, '%Y-%m-%d %H:%i:%s') tran_time_str");

        wrapper.orderByDesc("id");
        IPage<Map<String, Object>> pages = baseSqlService.getDataIPageWrapper("coin_log_transaction", page, wrapper);
        List<Map<String, Object>> records = pages.getRecords();
        for(Map<String,Object> dataMap:records){
            String token_id = MjkjUtils.getMap2Str(dataMap, "token_id");
            String buyHash = MjkjUtils.getMap2Str(dataMap, "hash");
            Date buyTime = MjkjUtils.getMap2DateTime(dataMap, "tran_time");
            String buyTimeStr = DateUtil.format(buyTime, "yyyy-MM-dd HH:mm:ss");
            String buyTranState = MjkjUtils.getMap2Str(dataMap, "tran_state");
            BigDecimal buyPrice = MjkjUtils.getMap2BigD(dataMap, "buy_price");
            String tokenAddress = MjkjUtils.getMap2Str(dataMap, "token_address");
            String userAddress = MjkjUtils.getMap2Str(dataMap, "user_address");
            Integer cou = MjkjUtils.getMap2Integer(dataMap, "good_wallet_cou");
            BigDecimal sellScale = MjkjUtils.getMap2BigD(dataMap, "sell_scale");


            dataMap.put("gdlx",cou==-99?"首页热门":"聪明钱包");
            dataMap.put("token_address_str",tokenAddress.substring(0,6) +"..."+tokenAddress.substring(tokenAddress.length()-6));
            dataMap.put("user_address_str",userAddress.substring(0,6) +"..."+userAddress.substring(userAddress.length()-6));

            //处理买入信息
            dataMap.put("buy_hash",buyHash);//卖出哈希
            dataMap.put("buy_hash_str",buyHash.substring(0,6) +"..."+buyHash.substring(buyHash.length()-6));//卖出哈希
            dataMap.put("buy_time",buyTimeStr);//卖出是
            dataMap.put("buy_tran_state",buyTranState);//卖出状态
            dataMap.put("buy_price",buyPrice.stripTrailingZeros().toPlainString());//卖出价格
            dataMap.put("buy_target_price",buyPrice.multiply(sellScale).stripTrailingZeros().toPlainString());

            //获取卖出信息
            QueryWrapper sellWrapper = MjkjUtils.getQueryWrapper();
            sellWrapper.eq("token_id",token_id);
            sellWrapper.eq("tran_type","SELL");
            List<Map<String, Object>> sellMapList = baseSqlService.getDataListByWrapper("coin_log_transaction", sellWrapper);
            if(Func.isNotEmpty(sellMapList)){
                Map<String, Object> sellMap = sellMapList.get(0);
                String sellHash = MjkjUtils.getMap2Str(sellMap, "hash");
                Date sellTime = MjkjUtils.getMap2DateTime(sellMap, "tran_time");
                String sellTimeStr = DateUtil.format(sellTime, "yyyy-MM-dd HH:mm:ss");
                String sellTranState = MjkjUtils.getMap2Str(sellMap, "tran_state");
                BigDecimal sellPrice = MjkjUtils.getMap2BigD(sellMap, "buy_price");
                BigDecimal subPrice = sellPrice.subtract(buyPrice);


                dataMap.put("sell_hash",sellHash);//卖出哈希
                dataMap.put("sell_hash_str",sellHash.substring(0,6) +"..."+sellHash.substring(sellHash.length()-6));//卖出哈希
                dataMap.put("sell_time",sellTimeStr);//卖出是
                dataMap.put("sell_tran_state",sellTranState);//卖出状态
                dataMap.put("sell_price",sellPrice.stripTrailingZeros().toPlainString());//卖出价格
                dataMap.put("sub_price",subPrice.stripTrailingZeros().toPlainString());//
            }else{
                dataMap.put("sell_hash","-");//卖出哈希
                dataMap.put("sell_time","-");//时间
                dataMap.put("sell_tran_state","-");//卖出状态
                dataMap.put("sell_price","-");//卖出价格
                dataMap.put("sub_price","-");//
            }

        }


        return getDataTable(pages);
    }




}